import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import processing.core.PApplet;


@SuppressWarnings("serial")
public class MobileVisual extends PApplet {
	Table data;
	String file = "data.csv";
	ArrayList<Node> listOfNodes;
	
	int MAX_Y_VALUE = 86400;
	
	int red 		= 0x75AA0000;
	int green		= 0x7500AA00;
	int blue 		= 0x750000AA;
	int selectColor = 0xA0FF8C00;
	
	boolean drawNodes = false;
	
	public void setup() {
		//size(800,600);
		size(1280,720);
		background(100);
		
		try {
			data = new Table(file);
		} catch (ParseException e) {
			System.out.println("Error loading the file: ");
			e.printStackTrace();
		}
		
		listOfNodes = new ArrayList<Node>();
		
		setupNodes("Voice");
		setupNodes("Text messages");
		setupNodes("Data");
	}
	
	
	public void draw() {
		background(100);
		drawVerticalLabels();		
	
		//if (drawNodes)
			//drawNode();
		//else
			//drawBins();
	
	}
	
	public void drawNode() {
		for (int i = 0; i < listOfNodes.size(); i++ ) {		
			// Pass in if mouse is over object and image
			Node n = listOfNodes.get(i);
			n.draw();
			n.mouseOver();
		}
	}
	
	@SuppressWarnings("deprecation")
	public void setupNodes(String usageType) {
		int numberOfRows = data.getRowCount();
		float maxTarget = data.getMaxDuration();
		float maxCurrent = 80;
		int sColor = 0;
		float d;
		
		if (usageType.equals("Data")) {
			maxTarget = data.getMaxDataVolume();
			maxCurrent = 500;
		} else {
			maxTarget = data.getMaxDuration();
			maxCurrent = 80;
		}
		
		for (int i = 0; i < numberOfRows; i++) {						
			if(data.getUsageType(i).equals(usageType)) {
				if (usageType.equals("Data"))
					d = data.getData(i);
				else 
					d = data.getDuration(i);
					
				// The 25 on the x & y is so that there is some padding along the edges
				float x = map(convertDateToSeconds(data.getDate(i)),0,MAX_Y_VALUE,25,width-25);
				float y = map(data.getDate(i).getDate(),1,31,25,height-110);
				int diameter = parseInt(map(d,0,maxCurrent,0,maxTarget));
				
				if (usageType.equals("Text messages"))
					diameter *= 10;
				else if (usageType.equals("Data"))
					diameter *= 10;
				
				if (data.getUsageType(i).equals("Text messages"))
					sColor = blue;
				else if (data.getUsageType(i).equals("Voice"))
					sColor = red;
				else if (data.getUsageType(i).equals("Data"))
					sColor = green; 

				Node instance = new Node(this, x, y, diameter, data.getUsageType(i), data.getDate(i).toString(),sColor);
				listOfNodes.add(instance);		
			}
		}
	}
	
	public void drawBins() {
		String node;
		int numOfVoice = 0, numOfData =0;
		int vDiameter = 0, tDaimeter = 0, dDiameter = 0;
		
		textSize(10);
		textAlign(CENTER);
		
		DecimalFormat df = new DecimalFormat();
		df.setMinimumFractionDigits(2);
		
		for (int i = 0; i < listOfNodes.size();i++) {
			node = listOfNodes.get(i).getType();

			if (node.equals("Voice") && !listOfNodes.isEmpty()) {
				vDiameter += listOfNodes.get(i).getDiameter();;
				numOfVoice++;
			} else if (node.equals("Data") && !listOfNodes.isEmpty()) { 
				dDiameter += listOfNodes.get(i).getDiameter();
				numOfData++;
			} else if (node.equals("Text messages") && !listOfNodes.isEmpty()) {
				tDaimeter += listOfNodes.get(i).getDiameter();
			}
		}
		
		fill(red);
		ellipse(220,360,vDiameter/10,vDiameter/10);
		fill(255);
		text("Minutes: " + vDiameter,220,360);
		text("# of calls: " + numOfVoice,220,370);
		fill(green);
		ellipse(1100,360,dDiameter/10,dDiameter/10);
		fill(255);
		text("MBs of data: " + dDiameter,1100,360);
		fill(blue);
		ellipse(680,360,tDaimeter/100,tDaimeter/100);
		fill(255);
		text("Number of texts: " + tDaimeter/10,680,360);
		text("Average number of texts per day: " + data.getAverageTextPerDay(),680,380);
	}
	
	public void drawVerticalLabels() {
		int numberOfHoursPerDay = 24;
		float x = 0;
		int y1 = 0;
		int y2 = 720;
		int time = 0;
		
		for (int i = 0; i <= numberOfHoursPerDay; i++) {
			stroke(80);
			line(x,y1,x,y2);

			textAlign(CENTER,BOTTOM);
			
			pushMatrix();
			translate(30,670);
			rotate(-HALF_PI);

			textSize(30);
			fill(80);
			text(time + ":00",0,x+15);
			popMatrix();
			
			x += 53.3333;
			time++;
		}
	}

	/**
	 * Converts time 24 hour time into seconds so that it can be used as a scale for the x-axis
	 * @param date	Date object
	 * @return		time represented in seconds
	 */
	public int convertDateToSeconds(Date date) {
		SimpleDateFormat fmt = new SimpleDateFormat("HH:mm");
		String time = fmt.format(date);
		String tokens[] = time.split(":");
		String hour = tokens[0];
		String minute = tokens[1];
		
		int secondsOfHour = Integer.parseInt(hour) * 3600; 
		int secondsOfMinute = Integer.parseInt(minute) * 60;

		return secondsOfHour + secondsOfMinute;
	}
	
	public void keyPressed() {
		if (key == '1') 
			if (drawNodes == false)
				drawNodes = true;
			else 
				drawNodes = false;
	}
	
	public static void main(String args[]) {
		PApplet.main(new String[] {"--bgcolor=#F0F0F0", "CPSC 581 - Project 2 - Mobile Device Usage"});
	}

}
	